<template>
  <div class="equip">
    <!-- 搜索和分类标签区域 -->
    <div class="search-section">
      <input
        class="input"
        v-model="keywords"
        type="text"
        placeholder="请输入您要搜索的装备名"
        @input="handleSearchInput"
      />


    <div class="filter-tabs">
        <div 
          :class="['filter-tab', { active: activeCategory === 'all' }]"
          @click="setCategory('all')"
        >
          全部
        </div>
        <div 
          :class="['filter-tab', { active: activeCategory === 'basic' }]"
          @click="setCategory('basic')"
        >
          基础装备
        </div>
        <div 
          :class="['filter-tab', { active: activeCategory === 'combined' }]"
          @click="setCategory('combined')"
        >
          合成装备
        </div>
        <div 
          :class="['filter-tab', { active: activeCategory === 'artifact' }]"
          @click="setCategory('artifact')"
        >
          神器
        </div>
      </div>
    </div>

     <!-- 装备列表 -->
    <div class="list">
      <div 
        class="item" 
        :title="item.name" 
        v-for="item in filteredEquips"
        :key="item.id"
        @click="goToEquipDetail(item.id)"
      >
        <!-- 装备类型标识 -->
        <div 
          class="grading-badge" 
          :class="{
            'basic-badge': item.id >= 1 && item.id <= 6,
            'combined-badge': item.id >= 7 && item.id <= 27,
            'artifact-badge': item.id >= 28
          }"
        >
          {{ getBadgeText(item.id) }}
        </div>

        <!-- 使用后端返回的图片URL -->
        <img v-if="item.img" :src="item.img" :alt="item.name" />
        <img v-else src="../assets/default_equip.png" :alt="item.name" />

        <div class="name">{{ item.name }}</div>
        <!-- 装备信息弹窗 -->
        <el-tooltip
          :content="item.description"
          raw-content
        >
          <div class="mask">
            
          </div>
        </el-tooltip>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted,computed} from "vue";
import { useRouter } from "vue-router";
import request from '@/utils/request'

const router = useRouter();

const state = reactive({
  equips: [],
  equipsAll: [], // 存储所有装备数据
  keywords: "",
  activeCategory: "all" // 当前选中的分类
});


// 获取所有装备
const fetchEquips = async () => {
  try {
    const response = await request({
      url: '/equip',
      method: 'get'
    });
    state.equipsAll = response;
  } catch (error) {
    console.error('获取装备列表失败:', error);
  }
};

// 根据ID范围筛选装备类型
const filteredEquips = computed(() => {
  // 首先应用关键词过滤
  let filtered = state.equipsAll;
  if (state.keywords.trim()) {
    const keyword = state.keywords.trim().toLowerCase();
    filtered = filtered.filter(item => 
      item.name.toLowerCase().includes(keyword)
    );
  }
  
  // 然后应用类别过滤
  switch (state.activeCategory) {
    case "basic":
      return filtered.filter(item => item.id >= 1 && item.id <= 6);
    case "combined":
      return filtered.filter(item => item.id >= 7 && item.id <= 27);
    case "artifact":
      return filtered.filter(item => item.id >= 28);
    default:
      return filtered;
  }
});
// 设置装备分类
const setCategory = (category) => {
  state.activeCategory = category;
};

// 获取徽章文本
const getBadgeText = (id) => {
  if (id >= 1 && id <= 6) return "基础";
  if (id >= 7 && id <= 27) return "合成";
  return "神器";
};

// 防抖搜索处理
let searchTimer = null;
const handleSearchInput = () => {
  clearTimeout(searchTimer);
  searchTimer = setTimeout(() => {
    // 这里不再需要单独的搜索函数，因为计算属性会自动更新
  }, 300);
};

// 跳转到装备详情页
const goToEquipDetail = (id) => {
  router.push({ path: '/equipdetail', query: { id: id } });
};

onMounted(() => {
  fetchEquips();
});

const { equips, keywords } = toRefs(state);
</script>

<style lang="less" scoped>
.equip {
  position: relative;
  min-height: 100vh;
  margin-left: 20px;
  margin-top: 20px;
  margin-right: 20px;
  
  &::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url('../assets/heard(2).png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    opacity: 0.3;
    z-index: -1;
  }
  
  .search-section {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    gap: 15px;
    flex-wrap: wrap;
  }
  
  .input {
    height: 40px;
    width: 250px;
    padding: 0 15px;
    border: 2px solid #00a383;
    border-radius: 20px;
    font-size: 16px;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(5px);
    color: #333;
  }

  .filter-tabs {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
  }
  
  .filter-tab {
    padding: 8px 16px;
    border-radius: 20px;
    background: rgba(0, 163, 131, 0.2);
    border: 1px solid rgba(0, 163, 131, 0.5);
    color: #ccc;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 14px;
    user-select: none;
    
    &:hover {
      background: rgba(0, 163, 131, 0.3);
    }
    
    &.active {
      background: rgba(0, 163, 131, 0.8);
      color: white;
      border-color: #00a383;
      box-shadow: 0 0 10px rgba(0, 163, 131, 0.5);
    }
  }

  .list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 15px;
    
    .item {
      position: relative;
      cursor: pointer;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 10px;
      background: rgba(30, 35, 40, 0.8);
      border-radius: 8px;
      transition: all 0.3s ease;
      border: 2px solid transparent;
      
      &:hover {
        transform: translateY(-5px);
        border-color: #00a383;
        box-shadow: 0 5px 15px rgba(0, 163, 131, 0.3);
      }
      
      .grading-badge {
        position: absolute;
        top: 5px;
        left: 5px;
        color: white;
        font-size: 10px;
        padding: 2px 5px;
        border-radius: 4px;
        z-index: 1;
        font-weight: bold;
      }
      
      .basic-badge {
        background: rgba(75, 175, 80, 0.8); /* 绿色 */
      }
      
      .combined-badge {
        background: rgba(33, 150, 243, 0.8); /* 蓝色 */
      }
      
      .artifact-badge {
        background: rgba(255, 152, 0, 0.8); /* 橙色 */
      }
      
      img {
        width: 100px;
        height: 100px;
        border-radius: 0;
        border: 2px solid #00a383;
        object-fit: cover;
      }
      
      .name {
        margin-top: 8px;
        color: #fff;
        font-size: 14px;
        font-weight: 600;
        text-align: center;
      }
      
      .mask {
        position: absolute;
        top: 5px;
        right: 5px;
        width: 25px;
        height: 25px;
        background: #00a383;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        opacity: 0.8;
        
        img {
          width: 15px;
          height: 15px;
          border: none;
          margin: 0;
        }
      }
    }
  }
}

@media (max-width: 1600px) {
  .list {
    grid-template-columns: repeat(6, 1fr);
  }
}

@media (max-width: 1200px) {
  .list {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 768px) {
  .list {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .search-section {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .input {
    width: 100%;
  }
  
  .filter-tabs {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .list {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .filter-tab {
    padding: 6px 12px;
    font-size: 13px;
  }
  
  .equip {
    margin-left: 10px;
    margin-right: 10px;
  }
}
</style>
